package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.service.ZhongXingJSBDJService;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.infra.cda.zxjsb.JingShenBDJVo;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.ZhongXingJSBDJ;
import com.jy.rhin.model.body.zhongxingjsbdj.*;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ZhongXingJSBDJApplication {

    @Autowired
    private CDACreator cdaCreator;
    @Autowired
    private ZhongXingJSBDJService zhongXingJSBDJService;
    /*@Autowired
    private DataMigrationAPI dataMigrationAPI;*/
    @Autowired
    private ShuYuService shuYuService;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String jingShenBJLID, String yeWuJGDM) {
        return cdaCreator.create(createZhongXingJSBDJ(jingShenBJLID, yeWuJGDM), "14zhongxingjsbdj.ftl");
    }

    public ClinicalDocumentData createZhongXingJSBDJ(String jingShenBJLID, String yeWuJGDM) {
        JingShenBDJVo jingShenBDJVo = zhongXingJSBDJService.get(jingShenBJLID, yeWuJGDM);
        if (Objects.isNull(jingShenBDJVo)) {
            throw new CDAException("主业务数据不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(yeWuJGDM).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(yeWuJGDM).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }

        shuYuZhiYuQuery(jingShenBDJVo);

        ZhongXingJSBDJ docData = new ZhongXingJSBDJ();
        docData.setId(jingShenBJLID);
        setClinicalDocumentData(docData, jingShenBDJVo, hospital);

        ZhiQingTY zhiQingTY = new ZhiQingTY();
        zhiQingTY.setGuanLiBZ(Integer.valueOf(1).equals(jingShenBDJVo.getZHIQINGTYSGLBZ()) ? "true" : "false");
        zhiQingTY.setQianZiRQ(CDAUtil.toD8(jingShenBDJVo.getTONGYISHUQZRQ()));
        zhiQingTY.setQianZiRXM(jingShenBDJVo.getTONGYISHUQZRXM());
        docData.setZhiQingTY(zhiQingTY);

        JianKangShi jianKangShi = new JianKangShi();
        jianKangShi.setShouCiFBNL(Converter.toString(jingShenBDJVo.getSHOUCIFBNL()));
        jianKangShi.setJingShenZZ(new CodeObject(jingShenBDJVo.getJINGSHENZZDM(), jingShenBDJVo.getJINGSHENZZMC()));
        if (!StringUtil.isNullOrEmpty(jingShenBDJVo.getJIWANGMZZLQKDM())) {
            jianKangShi.setJiWangZLQK(new CodeObject(jingShenBDJVo.getJIWANGMZZLQKDM(), jingShenBDJVo.getJIWANGMZZLQKMC()));
        }
        jianKangShi.setShouCiKJSBZLRQ(CDAUtil.toD8(jingShenBDJVo.getSHOUCIKJSBYWZLRQ()));
        jianKangShi.setJiWangJSZKZYCS(Converter.toString(jingShenBDJVo.getJIWANGJSZKZYCS()));
        jianKangShi.setJiWangZLXG(new CodeObject(jingShenBDJVo.getJIWANGZLXGLBDM(), jingShenBDJVo.getJIWANGZLXGLBMC()));
        docData.setJianKangShi(jianKangShi);

        ZhuYaoJKWT zhuYaoJKWT = new ZhuYaoJKWT();
        zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject(jingShenBDJVo.getDUIJIATINGSHYXDM(), jingShenBDJVo.getDUIJIATINGSHYXMC()));
        zhuYaoJKWT.setHuanBingSHYXCS(Converter.toString(jingShenBDJVo.getDUIJIATINGSHYXCS()));
        zhuYaoJKWT.setGuanSuoQK(new CodeObject(Converter.toString(jingShenBDJVo.getGUANSUOQKDM()), jingShenBDJVo.getGUANSUOQKMC()));
        zhuYaoJKWT.setJingJiQK(new CodeObject(Converter.toString(jingShenBDJVo.getJINGJIZKDM()), jingShenBDJVo.getJINGJIZKMC()));
        docData.setZhuYaoJKWT(zhuYaoJKWT);

        if (!StringUtil.isNullOrEmpty(jingShenBDJVo.getZHONGXINGJSJBDM())) {
            ZhenDuanJL zhenDuanJL = new ZhenDuanJL();
            zhenDuanJL.setQueZhenRQ(CDAUtil.toD8(jingShenBDJVo.getQUEZHENRQ()));
            zhenDuanJL.setJiBingDM(new CodeObject(Converter.toString(jingShenBDJVo.getZHONGXINGJSJBDM()), jingShenBDJVo.getZHOGNXINGJSJBMC()));
            zhenDuanJL.setQueZhenYLJGMC(jingShenBDJVo.getQUEZHENYLJGMC());
            docData.setZhenDuanJL(zhenDuanJL);
        }

        if (!StringUtil.isNullOrEmpty(jingShenBDJVo.getZHUANKEYSYJ())) {
            JianKangPG jianKangPG = new JianKangPG();
            jianKangPG.setZhuanKeYSYJ(jingShenBDJVo.getZHUANKEYSYJ());
            docData.setJianKangPG(jianKangPG);
        }

        return docData;
    }

    private void shuYuZhiYuQuery(JingShenBDJVo jingShenBDJVo) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getJIANHURENYBRGXMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GB_T_4761.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBDJVo.getJIANHURENYBRGXDM()))));
        }
        if (!StringUtil.isNullOrEmpty(jingShenBDJVo.getJIWANGMZZLQKDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_MZZLQKDMB.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBDJVo.getJIWANGMZZLQKDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getJIWANGZLXGLBMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_10_010.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBDJVo.getJIWANGZLXGLBDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getDUIJIATINGSHYXMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV03_00_112.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBDJVo.getDUIJIATINGSHYXDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getGUANSUOQKMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_GSQKDMB.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBDJVo.getGUANSUOQKDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getJINGJIZKMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_JJQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBDJVo.getJINGJIZKDM()))));
        }
        if (!StringUtil.isNullOrEmpty(jingShenBDJVo.getZHONGXINGJSJBDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_01_036.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBDJVo.getZHONGXINGJSJBDM()))));
        }

        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getJIANHURENYBRGXMC())) {
            jingShenBDJVo.setJIANHURENYBRGXMC(ZhiJiUtils.getValue(HealthDictionaryEnum.GB_T_4761.getCode(), Converter.toString(jingShenBDJVo.getJIANHURENYBRGXDM()), valueByCode));
        }
        if (!StringUtil.isNullOrEmpty(jingShenBDJVo.getJIWANGMZZLQKDM())) {
            jingShenBDJVo.setJIWANGMZZLQKMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_MZZLQKDMB.getCode(), Converter.toString(jingShenBDJVo.getJIWANGMZZLQKDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getJIWANGZLXGLBMC())) {
            jingShenBDJVo.setJIWANGZLXGLBMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_10_010.getCode(), Converter.toString(jingShenBDJVo.getJIWANGZLXGLBDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getDUIJIATINGSHYXMC())) {
            jingShenBDJVo.setDUIJIATINGSHYXMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV03_00_112.getCode(), Converter.toString(jingShenBDJVo.getDUIJIATINGSHYXDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getGUANSUOQKMC())) {
            jingShenBDJVo.setGUANSUOQKMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_GSQKDMB.getCode(), Converter.toString(jingShenBDJVo.getGUANSUOQKDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBDJVo.getJINGJIZKMC())) {
            jingShenBDJVo.setJINGJIZKMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_JJQK.getCode(), Converter.toString(jingShenBDJVo.getJINGJIZKDM()), valueByCode));
        }
        if (!StringUtil.isNullOrEmpty(jingShenBDJVo.getZHONGXINGJSJBDM())) {
            jingShenBDJVo.setZHOGNXINGJSJBMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_01_036.getCode(), Converter.toString(jingShenBDJVo.getZHONGXINGJSJBDM()), valueByCode));
        }
    }

    ZhongXingJSBDJ setClinicalDocumentData(ZhongXingJSBDJ clinicalDocumentData, JingShenBDJVo jingShenBDJVo, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(jingShenBDJVo.getLURURQ() != null ? jingShenBDJVo.getLURURQ() : jingShenBDJVo.getGENGXINSJ()));
        clinicalDocumentData.setPatientRole(createPatientRole(jingShenBDJVo));
        clinicalDocumentData.setAuthor(createAuthor(jingShenBDJVo, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setLegalAuthenticator(createLegalAuthenticator(jingShenBDJVo));
        clinicalDocumentData.setParticipant(createParticipant(jingShenBDJVo));
        return clinicalDocumentData;
    }

    Author createAuthor(JingShenBDJVo jingShenBDJVo, HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(CDAUtil.toD15(jingShenBDJVo.getLURURQ() != null ? jingShenBDJVo.getLURURQ() : jingShenBDJVo.getGENGXINSJ()));
        author.setId(!StringUtil.isNullOrEmpty(jingShenBDJVo.getLURUREN()) ? jingShenBDJVo.getLURUREN() : jingShenBDJVo.getLURURXM());
        author.setName(jingShenBDJVo.getLURURXM());
        author.setOrganizationId(CDAUtil.toJGID10(hospital.getOutId()));
        author.setOrganizationName(hospital.getName());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(CDAUtil.toJGID10(hospital.getOutId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    LegalAuthenticator createLegalAuthenticator(JingShenBDJVo jingShenBDJVo) {
        LegalAuthenticator legalAuthenticator = new LegalAuthenticator();
        legalAuthenticator.setName(jingShenBDJVo.getLURURXM());
        return legalAuthenticator;
    }

    Participant createParticipant(JingShenBDJVo jingShenBDJVo) {
        Participant participant = new Participant();
        participant.setTelecom(!StringUtil.isNullOrEmpty(jingShenBDJVo.getJUWEIHUILXRDH()) ? jingShenBDJVo.getJUWEIHUILXRDH() : "-");
        participant.setName(!StringUtil.isNullOrEmpty(jingShenBDJVo.getJUWEIHUILXXRXM()) ? jingShenBDJVo.getJUWEIHUILXXRXM() : "-");
        participant.setScopingOrgName(!StringUtil.isNullOrEmpty(jingShenBDJVo.getJUWEIHUIMC()) ? jingShenBDJVo.getJUWEIHUIMC() : "-");
        return participant;
    }

    PatientRole createPatientRole(JingShenBDJVo jingShenBDJVo) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(jingShenBDJVo.getGERENDABS()));
        patientRole.setPatient(createPatient(jingShenBDJVo));
        return patientRole;
    }

    Patient createPatient(JingShenBDJVo jingShenBDJVo) {
        Patient patient = new Patient();
        patient.setId(jingShenBDJVo.getZHENGJIANHM());
        patient.setName(jingShenBDJVo.getXINGMING());
        patient.setAdministrativeGender(new CodeObject(Converter.toString(jingShenBDJVo.getXINGBIEDM()), jingShenBDJVo.getXINGBIEMC()));
        patient.setBirthTime(CDAUtil.toD8(jingShenBDJVo.getCHUSHENGRQ()));
        patient.setGuardian(createGuardian(jingShenBDJVo));
        return patient;
    }

    Guardian createGuardian(JingShenBDJVo jingShenBDJVo) {
        Guardian guardian = new Guardian();
        guardian.setCode(new CodeObject(Converter.toString(jingShenBDJVo.getJIANHURENYBRGXDM()), jingShenBDJVo.getJIANHURENYBRGXMC()));
        guardian.setTelecom(jingShenBDJVo.getJIANHURENDH());
        guardian.setName(jingShenBDJVo.getJIANHURENXM());
        guardian.setAddr(createGuardianAddr(jingShenBDJVo));
        return guardian;
    }

    Addr createGuardianAddr(JingShenBDJVo jingShenBDJVo) {
        Addr addr = new Addr();
        addr.setState(jingShenBDJVo.getLIANXIRENDZSMC());
        addr.setCity(jingShenBDJVo.getLIANXIRENDZSHIMC());
        addr.setCounty(jingShenBDJVo.getLIANXIRENDZXIANMC());
        addr.setTownship(jingShenBDJVo.getLIANXIRENDZXZMC());
        addr.setStreetName(jingShenBDJVo.getLIANXIRENDZSQMC());
        addr.setHouseNumber(!StringUtil.isNullOrEmpty(jingShenBDJVo.getLIANXIRENDZQTDM()) ? jingShenBDJVo.getLIANXIRENDZQTDM() : jingShenBDJVo.getLIANXIRENDZXXMC());
        return addr;
    }

}
